package cn.lisens.demo.web;

import java.time.LocalTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.lisens.demo.entity.User;
import cn.lisens.demo.service.UserService;

@RestController(value = "user")
public class UserController {

	@Autowired
	UserService userService;
	
	@RequestMapping("insert")
	public String batchInert() {
		long startTime1s = System.currentTimeMillis();
		int execCount = 0;
		User last = new User();
		last.setId(0L);
		for (int i = 0; i < Integer.MAX_VALUE; i++) {

			User user = new User();
			user.setFullname("张三"+i);
			user.setIdcard("150101458455236471"+i);
			
//			long startTime = System.currentTimeMillis();
			userService.createUser(user);			 
			long endTime = System.currentTimeMillis();
//			long duration = (endTime - startTime);
//			System.out.println("执行时间: " + duration + " 毫秒");
			
			execCount++;
			if(endTime - startTime1s > 1000) {
				LocalTime now = LocalTime.now();
				// 获取当前线程ID
		        long threadId = Thread.currentThread().getId();
		        // 获取当前线程名称
		        String threadName = Thread.currentThread().getName();
				// 获取时分秒
		        int hour = now.getHour();    // 时 (0-23)
		        int minute = now.getMinute(); // 分 (0-59)
		        int second = now.getSecond(); // 秒 (0-59)
		        System.out.printf("当前时间: %02d:%02d:%02d，当前线程ID: %d, 线程名称: %s %n", hour, minute, second, threadId, threadName);
				System.out.println("1秒执行"+execCount+" 条，平均一条:"+((endTime - startTime1s)/execCount)+" ms，数据库主键："+user.getId()+"数据库1秒新增："+(user.getId()-last.getId()));
				execCount = 0;
				startTime1s = System.currentTimeMillis();
				last = user;
			}
		}
		return "finish";
	}
	
	@RequestMapping("get")
	public User get(Long id) {
		return userService.getUserById(id);			 
	}
	
	@RequestMapping("save")
	public String save(User user) {
		userService.createUser(user);
		return "保存成功";
	}
	
	@RequestMapping("")
	public String index() {
		return "index";
	}
}
